package org.hhh.pet.service.impl;

import org.hhh.basic.service.impl.BaseServiceImpl;
import org.hhh.basic.util.CodeGenerateUtils;
import org.hhh.basic.util.DistanceUtil;
import org.hhh.basic.util.PageList;
import org.hhh.basic.util.Point;
import org.hhh.order.domain.PetAcquisitionOrder;
import org.hhh.order.mapper.PetAcquisitionOrderMapper;
import org.hhh.org.domain.Employee;
import org.hhh.org.domain.Shop;
import org.hhh.org.mapper.EmployeeMapper;
import org.hhh.org.mapper.ShopMapper;
import org.hhh.pet.domain.Pet;
import org.hhh.pet.domain.SearchMasterMsg;
import org.hhh.pet.mapper.SearchMasterMsgMapper;
import org.hhh.pet.query.SearchMasterMsgQuery;
import org.hhh.pet.service.IPetService;
import org.hhh.pet.service.ISearchMasterMsgService;
import org.hhh.user.domain.Logininfo;
import org.hhh.user.domain.User;
import org.hhh.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private IPetService petService;

    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;


    @Override
    public void publish(SearchMasterMsg msg, Logininfo logininfo) {
        //获取用户信息
        //绑定用户
        User user = userMapper.loadByLogininfoId(logininfo.getId());
        msg.setUser_id(user.getId());
        //发布信息后寻找最近的门店绑定
        Point point = DistanceUtil.getPoint(msg.getAddress());
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.loadAll());
        msg.setShop_id(nearestShop.getId());
        //保存
        searchMasterMsgMapper.save(msg);
    }

    @Override
    public PageList<SearchMasterMsg> user(SearchMasterMsgQuery query, Logininfo logininfo) {
        User user = userMapper.loadByLogininfoId(logininfo.getId());
        if (user != null) {
            query.setUserId(user.getId());
        }
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Logininfo logininfo) {
        query.setState(0);//未处理寻主信息
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        if (employee.getShop_id() != null) {
            query.setShopId(employee.getShop_id());//查询任一一个店铺信息
        }
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Logininfo logininfo) {
        query.setState(1);//未处理寻主信息
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        if (employee.getShop_id() != null) {
            query.setShopId(employee.getShop_id());
        }
        return super.queryPage(query);
    }

    @Override
    public void handle(Pet pet, Logininfo logininfo) {
        //修改处理状态
        searchMasterMsgMapper.updateState2ById(pet.getSearch_master_msg_id());
        //生成宠物信息和详情信息
        petService.add(pet);
        //@TODO 创建收购订单
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(pet.getSearch_master_msg_id());
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        PetAcquisitionOrder order = pet2order(pet, searchMasterMsg,employee.getId());
        //保存收购订单
        petAcquisitionOrderMapper.save(order);
        //@TODO  生成支付单支付

    }


   private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt,Long empId) {
       PetAcquisitionOrder order = new PetAcquisitionOrder();
       order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
       order.setState(0);//待支付
       order.setPrice(pet.getCostprice());
       order.setAddress(adopt.getAddress());
       String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
       order.setOrderSn(orderSn);
       order.setPet_id(pet.getId());
       order.setUser_id(adopt.getUser_id());
       order.setPaytype(pet.getPayType());
       order.setShop_id(pet.getShop_id());
       order.setEmp_id(empId);
       return order;
    }
}
